package com.tbit.main.util;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.tbit.main.constant.AliOssConstant;
import com.tbit.main.pojo.OssConfig;
import net.coobird.thumbnailator.Thumbnails;

import java.io.*;
import java.util.UUID;


/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 10:50 2020/8/3
 */
public class OssFileUtils {

    public static Integer IMAGE = 2;

    public static Integer MP3 = 1;

    /**
     * @Description:
     * @Param: [inputStream, oosConfig, fileName, contentType, isMuisc(1代表map文件，2代表image)]
     * @return: java.lang.String
     * @Author: Jing
     * @Date: 2021/5/24
     */
    public static String uploadByAliYun(InputStream inputStream, OssConfig oosConfig, String fileName, String contentType, int isMuisc) throws Exception {
        /**判断文件大小*/
        //开始压缩图片
        if (isMuisc == 2) {
            inputStream = compress(inputStream);
        }
        //上传到阿里云
        OSSClient ossClient = new OSSClient(oosConfig.getEndpoint(), oosConfig.getAccessKeyId(), oosConfig.getAccessKeySecret());
        PutObjectRequest putObjectRequest = new PutObjectRequest(oosConfig.getBucketName(), fileName, inputStream);
        putObjectRequest.setMetadata(init(inputStream, fileName, contentType));
        // 上传文件。
        ossClient.putObject(putObjectRequest);
        ossClient.shutdown();
        return "http://" + oosConfig.getBucketName() + "." + oosConfig.getEndpoint() + "/" + fileName;
    }

    public static ObjectMetadata init(InputStream inputStream, String fileName, String contentType) throws IOException {
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(inputStream.available());
        objectMetadata.setCacheControl("no-cache");
        objectMetadata.setHeader("Pragma", "no-cache");
        objectMetadata.setContentType(getcontentType(contentType));
        objectMetadata.setContentDisposition("inline;filename=" + fileName);
        objectMetadata.setHeader("x-oss-forbid-overwrite", "false");
        return objectMetadata;
    }


    /**
     * Description: 判断OSS服务文件上传时文件的contentType
     *
     * @param FilenameExtension 文件后缀
     * @return String
     */
    public static String getcontentType(String FilenameExtension) {
        if ("bmp".equalsIgnoreCase(FilenameExtension)) {
            return "image/bmp";
        }
        if ("gif".equalsIgnoreCase(FilenameExtension)) {
            return "image/gif";
        }
        if ("jpeg".equalsIgnoreCase(FilenameExtension) ||
                "jpg".equalsIgnoreCase(FilenameExtension)
        ) {
            return "image/jpeg";
        }
        if ("png".equalsIgnoreCase(FilenameExtension)) {
            return "image/png";
        }
        if ("html".equalsIgnoreCase(FilenameExtension)) {
            return "text/html";
        }
        if ("txt".equalsIgnoreCase(FilenameExtension)) {
            return "text/plain";
        }
        if ("vsd".equalsIgnoreCase(FilenameExtension)) {
            return "application/vnd.visio";
        }
        if ("pptx".equalsIgnoreCase(FilenameExtension) ||
                "ppt".equalsIgnoreCase(FilenameExtension)) {
            return "application/vnd.ms-powerpoint";
        }
        if ("docx".equalsIgnoreCase(FilenameExtension) ||
                "doc".equalsIgnoreCase(FilenameExtension)) {
            return "application/msword";
        }
        if ("xml".equalsIgnoreCase(FilenameExtension)) {
            return "text/xml";
        }
        if ("mp3".equalsIgnoreCase(FilenameExtension)) {
            return "audio/mp3";
        }
        if ("mp4".equalsIgnoreCase(FilenameExtension)) {
            return "video/mpeg4";
        }
        if ("wav".equalsIgnoreCase(FilenameExtension)) {
            return "audio/wav";
        }
        return "image/jpeg";
    }


    /**
     * 压缩图片(没有原图)
     * 默认输出25%质量图片
     *
     * @param
     * @return
     * @throws IOException
     */
    public static InputStream compress(InputStream in) throws Exception {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        Thumbnails.of(in).scale(1f).outputQuality(0.25f).toOutputStream(os);
        InputStream is = new ByteArrayInputStream(os.toByteArray());
        in.close();
        os.close();
        return is;
    }

    public static void del(OssConfig oosConfig, String ossUrl) {
        //上传到阿里云
        OSSClient ossClient = new OSSClient(oosConfig.getEndpoint(), oosConfig.getAccessKeyId(), oosConfig.getAccessKeySecret());
        ossClient.deleteObject(oosConfig.getBucketName(), ossUrl);
    }

    public static void main(String[] args) throws Exception {
        InputStream inputStream = new FileInputStream("C:/Users/NING MEI/Desktop/1657854410581.jpg");
        OssConfig ossConfig = new OssConfig(AliOssConstant.endpoint, AliOssConstant.accessKeyId, AliOssConstant.accessKeySecret,
                AliOssConstant.bucketName);
        String imageId = UUID.randomUUID().toString();
        System.out.println(imageId);
        String fileName = OssFileUtils.uploadByAliYun(inputStream, ossConfig, imageId, "png", IMAGE);
        System.out.println(fileName);

        del(ossConfig, imageId);
    }
}
